﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JBeamLibrary.Events;
using JBeamLibrary.VehicleParts.Elements;
using System.Collections.ObjectModel;
using JBeamLibrary.VehicleParts.Interfaces;

namespace JBeamLibrary.VehicleParts.Collections
{
    public class FlexbodyCollection : KeyedCollection<string, Flexbody>, IVehicleGroupCollection<Flexbody>
    {
        #region Fields

        public event ElementPropertyChangedEventHandler ElementPropertyChanged;

        #endregion

        #region CTORs

        /// <summary>Creates a new mesh links collection</summary>
        public FlexbodyCollection()
        {

        }

        /// <summary>Creates a new mesh links collection</summary>
        /// <param name="flexbodies"></param>
        public FlexbodyCollection(IEnumerable<Flexbody> flexbodies)
        {
            AddRange(flexbodies);
        }

        #endregion

        #region Methods

        public new void Add(Flexbody flexbody)
        {
            base.Add(flexbody);

            flexbody.PropertyChanged += (s, e) =>
            {
                if (ElementPropertyChanged != null)
                    ElementPropertyChanged(s, e);
            };
        }

        public void Add(string mesh, params string[] groups)
        {
            Add(new Flexbody(mesh, groups));
        }

        public void Add(string mesh, IEnumerable<string> groups)
        {
            Add(new Flexbody(mesh, groups.ToArray()));
        }

        public void AddRange(IEnumerable<Flexbody> flexbodies)
        {
            foreach (var flexbody in flexbodies)
                Add(flexbody);
        }

        public void UpdateAdd(string mesh, params string[] groups)
        {
            UpdateAdd(new Flexbody(mesh, groups));
        }

        public void UpdateAdd(string mesh, IEnumerable<string> groups)
        {
            UpdateAdd(new Flexbody(mesh, groups.ToArray()));
        }

        public void UpdateAdd(Flexbody flexbodies)
        {
            if (Contains(flexbodies.Mesh))
                Remove(flexbodies.Mesh);
            Add(flexbodies);
        }

        public void UpdateAddRange(IEnumerable<Flexbody> flexbodies)
        {
            foreach (var flexbody in flexbodies)
                UpdateAdd(flexbody);
        }

        public new void Remove(Flexbody element)
        {
            base.Remove(element);
        }

        #endregion

        #region KeyedCollection

        protected override string GetKeyForItem(Flexbody item)
        {
            return item.Mesh;
        }

        #endregion
    }
}
